package com.qczl.pme.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.qczl.core.controller.BaseController;
import com.qczl.core.model.Json;
import com.qczl.pme.pojo.User;
import com.qczl.pme.service.UserService;

/**
 * 用户信息控制器
 * @author SongR
 *
 */
@Controller
@RequestMapping(value="/user")
public class UserController extends BaseController{
	
	@Resource
	UserService uService;
	
	/**
	 * 用户登录
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public Json login(HttpSession session,User u){
		Json j = new Json();
		try {
			User user = uService.login(u);
			if(null==user){
				j.setCode(1);
				j.setMsg("用户名或密码错误！");
			}else{
				j.setCode(0);
				j.setData(user);
				j.setMsg("登录成功");
			}
		} catch (Exception e) {
			j.setCode(-1);
			logger.error(e.toString(),e);
		}
		return j;
	}
	
	/**
	 * 退出登录
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/logout",method={RequestMethod.GET,RequestMethod.POST})
	public Json logout(HttpSession session,User u){
		Json j = new Json();
		try {
			String uStr = session.getAttribute("user").toString();
			User user = (User) JSONObject.parse(uStr);
			if(null!=user&&null!=u&&u.getId().equals(user.getId())){
				session.removeAttribute("user");
				j.setMsg("注销成功！");
				j.setCode(0);
			}else{
				j.setMsg("用户信息不存在！");
				j.setCode(1);
			}
		} catch (Exception e) {
			j.setCode(-1);
			logger.error(e.toString(),e);
		}
		return j;
	}
}
